package com.example.servlet;


import com.alibaba.fastjson.JSON;
import com.example.dao.PostDao;
import com.example.dao.UserDao;
import com.example.dao.imp.PostDaoImp;
import com.example.dao.imp.UserDaoImp;
import com.example.domain.Post;
import com.example.utils.ResponseUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "PostServlet", value = "/postServlet")
public class PostServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String postContent = request.getParameter("keyword");
        // 分页查询操作
        int page = Integer.parseInt(request.getParameter("page"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        PageHelper.startPage(page, pageSize);

        PostDao postDao = new PostDaoImp();
        List<Post> postList;


        int type = 0;
        String typeStr = request.getParameter("type");
        int userId = 0;
        String userIdStr = request.getParameter("userId"); // 获取用户ID
        if (typeStr != null ) {
            type = Integer.parseInt(typeStr);
        }
        if (userIdStr != null) {
            userId = Integer.parseInt(userIdStr);
        }
        if (type == 1) {
            // 根据用户ID和分类查询帖子
            int categoryId = Integer.parseInt(request.getParameter("categoryId"));
            postList = postDao.getPostsByUserIdAndCategoryId(userId, categoryId);
        } else if (type == 2) {
            // 仅根据用户ID查询帖子
            postList = postDao.getPostsByUserId(userId);
        } else if (type == 3) {
            postList = postDao.getPostsByUserIdAndContent(userId,postContent);
        }else {
            // 其他类型，直接查询所有帖子
            if (postContent == null || postContent.isEmpty()) {
                postList = postDao.getAllPosts();
            } else {
                postList = postDao.searchPostsByContent(postContent);
            }
        }

        PageInfo<Post> postPageInfo = new PageInfo<>(postList);
        ResponseUtil.sendSuccessResponse(response, 200, postPageInfo);
    }

    //增加操作
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        StringBuilder jsonBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonBuilder.append(line);
        }
        reader.close();

        Post post = JSON.parseObject(jsonBuilder.toString(), Post.class);
        PostDao postDao = new PostDaoImp();
        postDao.addPost(post);
        ResponseUtil.sendSuccessResponse(response, 200, "帖子添加成功");
    }


    /**
     * 帖子删除操作
     */
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        if (id != null) {
            int postId = Integer.parseInt(id);
            PostDao postDao = new PostDaoImp();
            postDao.deletePost(postId);
            ResponseUtil.sendSuccessResponse(response, 200, "帖子删除成功");
        } else {
            ResponseUtil.sendErrorResponse(response, 400, "帖子ID不能为空");
        }
    }
}
